/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
// 利用节点是不同的这条关键性质
/**
 * @param {TreeNode} root
 * @param {number[]} voyage
 * @return {number[]}
 */
var flipMatchVoyage = function (root, voyage) {
  let i = 0;
  let flipList = [];
  if (root.val !== voyage[i]) {
    return [-1];
  }
  const preOrder = (node) => {
    i++;
    if (!node.left && !node.right) {
      return;
    }
    if (node.left && node.left.val === voyage[i]) {
      preOrder(node.left);
    }

    const notMatch = node.left && node.left.val !== voyage[i];
    if (node.right && node.right.val === voyage[i]) {
      preOrder(node.right);
      if (notMatch) {
        if (node.left.val === voyage[i]) {
          flipList.push(node.val);
          preOrder(node.left);
        } else {
          flipList = [-1];
        }
      }
    } else if (notMatch) {
      flipList = [-1];
    }
  };
  preOrder(root);
  return flipList;
};
